<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/22
 * Time: 上午8:21
 */
namespace app\bussiness\controller;

use think\Exception;
use think\Db;

class Agreement extends Common
{
    //合约列表
    public function index()
    {
        $this->templateTitle('信息互通合约列表');
        $this->assign('status_list',get_bussiness_agreement_status('',2));

        $type = request()->param('type');
        $type = in_array($type,[1,2]) ? $type : 1;
        $this->assign('type',$type);

        //判断条件
        $where['ba.id']=['gt',0];

        $status = request()->param('status');
        $this->assign('status',$status);
        if($status!=''){
            $where['ba.status'] = $status;
        }

        //搜索条件
        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["bs.title|bs.id"] = $condition;
        }

        if($type==1){
            //我方收到的
            $where['ba.receiver_bus_id'] = $this->currentBussinessId();

            //获取列表
            $list = Db::name("bussiness_agreement")
                ->alias('ba')
                ->join('__BUSSINESS__ bs','bs.id=ba.sender_bus_id','LEFT')
                ->where($where)
                ->order("ba.update_time DESC")
                ->field('ba.*,bs.title as bsname')
                ->paginate(10);

        }else{
            $where['ba.sender_bus_id'] = $this->currentBussinessId();

            //获取列表
            $list = Db::name("bussiness_agreement")
                ->alias('ba')
                ->join('__BUSSINESS__ bs','bs.id=ba.receiver_bus_id','LEFT')
                ->where($where)
                ->order("ba.update_time DESC")
                ->field('ba.*,bs.title as bsname')
                ->paginate(10);
        }


        $this->assign("list", $list);

        return $this->fetch();
    }

    public function status()
    {
        if (request()->isAjax())
        {

            $id = request()->param("id");

            $agreement = Db::name('bussiness_agreement')
                ->where('id',$id)
                ->find();
            if(!$agreement){
                return $this->ajaxError('条约数据不存在或已删除');
            }
            if($agreement['receiver_bus_id'] != $this->currentBussinessId()){
                return $this->ajaxError('只能处理我方收到的合约');
            }
            $status = $agreement['status']==0?1:0;

            Db::name("bussiness_agreement")
                ->where("id", $id)
                ->update(array(
                    "status" => $status,
                    "update_time" => now_datetime(),
                ));

            $this->ajaxSuccess('状态修改成功');
        }
    }

    //删除
    public function remove()
    {
        if (request()->isAjax())
        {

            $id = request()->param("id");

            try
            {

                $del = Db::name("bussiness_agreement")
                    ->where("id", $id)
                    ->delete();

                if($del){
                    $this->ajaxSuccess('合约删除成功');
                }else{
                    $this->ajaxError('合约删除失败');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }


}